package com.rainbow.common.remote.feign;
import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate;
import com.rainbow.common.core.error.SentinelErrorUtil;
import okhttp3.OkHttpClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * 采用okhttp配置RestTemplate
 * @author 付为地
 */
@Configuration
public class RestTemplateConfig {

    @Resource
    private OkHttpClient okHttpClient;

    /**
     * 配置RestTemplate
     *
     * @return
     */
    @Bean
    @Primary
    @LoadBalanced
    @SentinelRestTemplate(blockHandler = "globalHandleFuse", blockHandlerClass = SentinelErrorUtil.class,fallback = "globalHandleFallback", fallbackClass = SentinelErrorUtil.class)
    public RestTemplate restTemplate() {
        return new RestTemplate(new OkHttp3ClientHttpRequestFactory(okHttpClient));
    }
}
